package com.lemon.ui.codemaker;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import org.assertj.core.util.Lists;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.lemon.components.ant.apimaker.SQLMaker;
import com.lemon.components.ant.apimaker.dto.ApiMakerReq;
import com.lemon.components.ant.apimaker.enums.CodePart;
import com.lemon.components.ant.apimaker.enums.FileWriteMode;
import com.lemon.components.ant.apimaker.enums.ResultType;
import com.lemon.components.ant.util.ConfigUtil;
import com.lemon.components.ant.util.sqlparser.CustomerMySqlASTVisitorAdapter.SQLTableInfo;
import com.lemon.ui.codemaker.component.RecentlyFile;
import com.system.core.security.NameUtil;

public class SQLMakerUI extends JDialog {

	private static final long serialVersionUID = 738342312417905144L;
	private final JPanel contentPanel = new JPanel();
	private final JTextArea sqlstr;
	private JTextField servicePackageText;
	private JTextField bizNameText;
	private ButtonGroup buttonGroup;
	private ButtonGroup buttonGroup2;
	private JTextField serviceNameText;
	private JTextField commentsText;
	JCheckBox chckbxNewCheckBox = new JCheckBox("DAO");
	JCheckBox chckbxNewCheckBox_1 = new JCheckBox("Service");
	JCheckBox chckbxNewCheckBox_2 = new JCheckBox("Controller");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SQLMakerUI dialog = new SQLMakerUI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SQLMakerUI() {
		setTitle("SQL-JAVA代码生成工具");
		setType(Type.UTILITY);
		setBounds(100, 100, 570, 555);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(53dlu;default)"),
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(120dlu;default)"),
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(38dlu;default)"),
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(133dlu;default)"),},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(124dlu;default):grow"),
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		{
			JLabel lblNewLabel = new JLabel("SQL语句");
			contentPanel.add(lblNewLabel, "2, 2, center, center");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "4, 2, 5, 1, fill, fill");
			{
				sqlstr = new JTextArea();
				sqlstr.getDocument().addDocumentListener(new DocumentListener() {
					
					@Override
					public void removeUpdate(DocumentEvent e) {
					}
					
					@Override
					public void insertUpdate(DocumentEvent e) {
						try {
							var document = e.getDocument();
							SQLMaker sqlmaker = new SQLMaker();
							List <SQLTableInfo> list = sqlmaker.getTables(document.getText(0, document.getLength()));
							if(list!=null&&list.size()>0) {
								var table = list.stream().filter(item->item.isMainTable()==true).findAny().orElse(null);
								serviceNameText.setText(NameUtil._2JavaName(table.getTableName()));
								servicePackageText.setText(NameUtil._2JavaName(table.getTableName()).toLowerCase());
							}
						} catch (BadLocationException e1) {
							e1.printStackTrace();
						}
					}
					
					@Override
					public void changedUpdate(DocumentEvent e) {
					}
				});
				scrollPane.setViewportView(sqlstr);
			}
		}
		{
			JLabel lblNewLabel_4 = new JLabel("结果集类型");
			contentPanel.add(lblNewLabel_4, "2, 4, right, default");
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, "4, 4, 5, 1, left, center");
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			{
				JRadioButton rdbtnNewRadioButton = new JRadioButton("单条");
				rdbtnNewRadioButton.setActionCommand("ResultSingle");
				panel.add(rdbtnNewRadioButton);
				JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("多条");
				rdbtnNewRadioButton_1.setActionCommand("ResultSet");
				rdbtnNewRadioButton_1.setSelected(true);
				panel.add(rdbtnNewRadioButton_1);
				buttonGroup = new ButtonGroup();
				buttonGroup.add(rdbtnNewRadioButton);
				buttonGroup.add(rdbtnNewRadioButton_1);
			}
		}
		{
			JLabel lblNewLabel_6 = new JLabel("生成代码");
			contentPanel.add(lblNewLabel_6, "2, 6, right, default");
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, "4, 6, 3, 1, left, center");
			panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
			{
				chckbxNewCheckBox_1.setActionCommand("SERVICE");
				chckbxNewCheckBox_2.setActionCommand("APICONTROLLER");
				
				chckbxNewCheckBox.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						if(!chckbxNewCheckBox.isSelected()) {
							chckbxNewCheckBox_1.setSelected(false);
							chckbxNewCheckBox_2.setSelected(false);
						}
					}
				});
				panel.add(chckbxNewCheckBox);
				chckbxNewCheckBox_1.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						if(chckbxNewCheckBox_1.isSelected()) {
							chckbxNewCheckBox.setSelected(true);
						} else {
							chckbxNewCheckBox_2.setSelected(false);
						}
					}
				});
				panel.add(chckbxNewCheckBox_1);
			
				chckbxNewCheckBox_2.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						if(chckbxNewCheckBox_2.isSelected()) {
							chckbxNewCheckBox_1.setSelected(true);
						}
					}
				});
				panel.add(chckbxNewCheckBox_2);
			}
		}
		{
			JLabel lblNewLabel_7 = new JLabel("写入模式");
			contentPanel.add(lblNewLabel_7, "2, 8, right, default");
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, "4, 8, 3, 1, left, center");
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			{
				JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("文件追加");
				rdbtnNewRadioButton_2.setActionCommand("APPEND");
				rdbtnNewRadioButton_2.setSelected(true);
				panel.add(rdbtnNewRadioButton_2);
				JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("新建文件");
				rdbtnNewRadioButton_3.setActionCommand("NEW");
				panel.add(rdbtnNewRadioButton_3);
				buttonGroup2 = new ButtonGroup();
				buttonGroup2.add(rdbtnNewRadioButton_2);
				buttonGroup2.add(rdbtnNewRadioButton_3);
			}
		}
		{
			JLabel lblNewLabel_2 = new JLabel("业务名称");
			contentPanel.add(lblNewLabel_2, "2, 10, right, default");
		}
		{
			bizNameText = new JTextField();
			contentPanel.add(bizNameText, "4, 10, fill, default");
			bizNameText.setColumns(10);
		}
		{
		}
		{
			JLabel lblNewLabel_5 = new JLabel("注释");
			contentPanel.add(lblNewLabel_5, "6, 10, right, default");
		}
		{
			commentsText = new JTextField();
			contentPanel.add(commentsText, "8, 10, fill, default");
			commentsText.setColumns(10);
		}
		{
			JLabel lblNewLabel_8 = new JLabel("服务名");
			contentPanel.add(lblNewLabel_8, "2, 12, right, default");
		}
		serviceNameText = new JTextField();
		serviceNameText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				servicePackageText.setText(serviceNameText.getText().toLowerCase());
			}
		});
		contentPanel.add(serviceNameText, "4, 12, fill, default");
		serviceNameText.setColumns(10);
		{
			JLabel lblNewLabel_3 = new JLabel("服务包名");
			contentPanel.add(lblNewLabel_3, "6, 12, right, default");
		}
		{
			servicePackageText = new JTextField();
			contentPanel.add(servicePackageText, "8, 12, fill, default");
			servicePackageText.setColumns(10);
		}
		{
			JLabel lblNewLabel_9 = new JLabel("分页");
			contentPanel.add(lblNewLabel_9, "2, 14, right, default");
		}
			JCheckBox chckbxNewCheckBox_3 = new JCheckBox("添加分页");
			chckbxNewCheckBox_3.setSelected(true);
			contentPanel.add(chckbxNewCheckBox_3, "4, 14");
		{
			JLabel lblNewLabel_1 = new JLabel(".");
			contentPanel.add(lblNewLabel_1, "2, 18, right, default");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("生成代码");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						List<CodePart> codeParts = Lists.newArrayList();
						if(chckbxNewCheckBox.isSelected())
							codeParts.add(CodePart.DAO);
						if(chckbxNewCheckBox_1.isSelected())
							codeParts.add(CodePart.SERVICE);
						if(chckbxNewCheckBox_2.isSelected())
							codeParts.add(CodePart.APICONTROLLER);
						
						ApiMakerReq apiMakerReq = ApiMakerReq.builder()
								.projectInfo(ConfigUtil.getInstance(RecentlyFile.configFilePath).getProjectInfoNoTable())
								.sql(sqlstr.getText())
								.bizName(bizNameText.getText())
								.servName(serviceNameText.getText())
								.servicePackageName(servicePackageText.getText())
								.resultType(ResultType.valueOf(buttonGroup.getSelection().getActionCommand()))
								.fileWriteMode(FileWriteMode.valueOf(buttonGroup2.getSelection().getActionCommand()))
								.comments(commentsText.getText())
								.codepart(codeParts)
								.pager(chckbxNewCheckBox_3.isSelected())
								.build();
						
						
						SQLMaker sqlMaker = new SQLMaker();
						try {
							sqlMaker.makeApiService(apiMakerReq);
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				});
				okButton.setActionCommand("makecode");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("关闭");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
